// 导入vue
import Vue from 'vue'
// 导入vue-router
import VueRouter from 'vue-router'

// 集成VueRouter中间件
Vue.use(VueRouter)

// 导入login
import Login from "../components/account/Login"

import Welcome from "../components/welcome/welcome"

import Layout from "../components/layout/Layout"
import User from "../components/users/User"


// 创建路由对象,导出去
const router=new VueRouter({
    routes:[
        {path:"/",redirect:'/login'},
        {path:'/login',component:Login,meta:{unrequiresAuth: true}},
        {path:'/layout',component:Layout,children:[
            {path:'',component:Welcome},
            {path:'users',component:User}
        ]},
    ]
})

router.beforeEach((to, from, next) => {
    // console.log(to);
    
    if (to.meta.unrequiresAuth) {
        next()
      
      } else {
        if (localStorage.getItem("mytoken")) {
            next()
        }else{
           return router.push({path:"/login"})
        }
      }
    
  })

// 导出
 export default router;